package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.CustomLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoApiController {

	@Autowired
	private OrderInfoService orderInfoService;

	@CustomLogin
	@Operation(summary = "生成订单")
	@PostMapping("/trade")
	public Result trade(@RequestBody TradeVo vo) {
		OrderInfoVo resultVo = orderInfoService.trade(vo);
		return Result.ok(resultVo);
	}

	@CustomLogin
	@Operation(summary = "提交订单")
	@PostMapping("/submitOrder")
	public Result<Map<String, Object>> submitOrder(@RequestBody OrderInfoVo orderInfoVo) {
		orderInfoService.submitOrder(orderInfoVo);
		Map<String, Object> map = new HashMap<>();
		map.put("orderNo", orderInfoVo.getTradeNo());
		return Result.ok(map);
	}

	@Operation(summary = "订单支付成功后，修改订单状态")
	@GetMapping("/updateOrderStatus/{orderNo}")
	public Result updateOrderStatus(@PathVariable("orderNo") String orderNo) {
		orderInfoService.updateOrderStatus(orderNo);
		return Result.ok();
	}

	@Operation(summary = "根据订单流水号，查看订单详情")
	@GetMapping("/getOrderInfo/{orderNo}")
	public Result<OrderInfo> getOrderInfo(@PathVariable("orderNo") String orderNo) {
		OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
		return Result.ok(orderInfo);
	}

	@CustomLogin
	@Operation(summary = "分页查询登录用户的订单列表")
	@GetMapping("/findOrderPage/{pageNum}/{pageSize}")
	public Result<IPage<OrderInfo>> findOrderPage(@PathVariable("pageNum") Integer pageNum,
												  @PathVariable("pageSize") Integer pageSize) {
		IPage<OrderInfo> page = orderInfoService.findOrderPage(pageNum, pageSize);
		return Result.ok(page);
	}
}

